package at.sysarch.filter;

import java.awt.Dimension;
import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import at.sysarch.utils.ImageChangedEvent;
import at.sysarch.utils.ImageChangedListener;

import com.sun.media.jai.widget.DisplayJAI;

public class DisplayImage extends DisplayJAI implements ImageChangedListener {

	private static final long serialVersionUID = 1L;
	private int _displaySize = 150;

	public DisplayImage() {
		
		setMinimumSize(new Dimension(_displaySize, 100));
	}
	
	public void displayImage(PlanarImage image) {
		
		int width = image.getWidth();
		float ratio = (float) ((float)_displaySize / width);

		ParameterBlock pb = new ParameterBlock();
		pb.addSource(image); // The source image
		pb.add(ratio); // The xScale
		pb.add(ratio); // The yScale
		pb.add(0.0F); // The x translation
		pb.add(0.0F); // The y translation
//		pb.add(new InterpolationNearest()); // The interpolation
		
		// Create the scale operation
		PlanarImage result = JAI.create("scale", pb, null);
		set(result);
	}

	@Override
	public void imageChanged(ImageChangedEvent event) {
		displayImage(event.getImage());
	}
}
